package com.school.revervationSys.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.school.revervationSys.dto.LoginDto;
import com.school.revervationSys.dto.PasswordUpdateDto;
import com.school.revervationSys.dto.UserDto;
import com.school.revervationSys.entity.User;
import com.school.revervationSys.mapper.UserMapper;
import com.school.revervationSys.service.IUserService;
import com.school.revervationSys.service.MenuService;
import com.school.revervationSys.utils.BaseResponse;
import com.school.revervationSys.utils.ErrorCode;
import com.school.revervationSys.utils.ResultUtils;
import com.school.revervationSys.vo.LoginVo;
import com.school.revervationSys.vo.MenuVo;
import org.junit.platform.commons.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.security.MessageDigest;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author msj
 * @since 2024-04-13
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {


    @Autowired
    private UserMapper userMapper;

    @Autowired
    private MenuService menuService;
    public boolean addUser(User user) {
        return this.save(user);
    }

    public boolean updateUser(User user) {
        return this.updateById(user);
    }

    public boolean delete(Integer userId) {

        return this.removeById(userId);
    }

    public boolean batchDelete(List<Integer> ids) {

        return this.removeByIds(ids);
    }

    public User userInfo(Integer userId) {

        return this.getById(userId);
    }

    public BaseResponse updatePassword(PasswordUpdateDto param) {


        User user = this.getById(param.getUserId());
        if (user.getPassword().equals(param.getOldPassword())) {
            user.setPassword(param.getNewPassword());
            boolean b = this.updateById(user);
            if (b) {
                return ResultUtils.success("密码修改成功！");
            }
            return ResultUtils.error(ErrorCode.OPERATION_FAILED, "修改失败！");
        } else {
            return ResultUtils.error(ErrorCode.OPERATION_FAILED, "原密码错误！");
        }

    }

    @Override
    public BaseResponse login(LoginDto param) {

        String account=param.getAccount();
        String password=param.getPassword();
        String roleCode = param.getRoleCode();

        User user = userMapper.selectOne(new QueryWrapper<User>().lambda().eq(User::getAccount, account));
        if(user == null){
            return ResultUtils.error(ErrorCode.PARAMS_ERROR,"账号不存在");
        }
        if(password.equals(user.getPassword())){
            LoginVo loginVo=new LoginVo();
            List<MenuVo> menuVos = menuService.menuList(roleCode);
            loginVo.setUser(user);
            loginVo.setMenuInfo(menuVos);
            return ResultUtils.success(loginVo);
        }

        return ResultUtils.error(ErrorCode.PARAMS_ERROR,"密码错误");
    }

    public List<User> userList(UserDto param) {

        LambdaQueryWrapper<User> queryWrapper = new QueryWrapper<User>().lambda()
                .eq(StringUtils.isNotBlank(param.getType()), User::getType, param.getType())
                .like(StringUtils.isNotBlank(param.getUserName()), User::getPhone, param.getPhone())
                .like(StringUtils.isNotBlank(param.getUserName()), User::getUserName, param.getUserName())
                .like(StringUtils.isNotBlank(param.getAccount()), User::getAccount, param.getAccount())
                .eq(StringUtils.isNotBlank(param.getSex()), User::getSex, param.getSex());

        List<User> users = userMapper.selectList(queryWrapper);
        return users;
    }

}
